package com.bdmc.web.controller.base;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bdmc.base.domain.bo.BaseDeviceBo;
import com.bdmc.base.domain.vo.BaseDeviceVo;
import com.bdmc.base.service.IBaseDeviceService;
import com.bdmc.common.annotation.Log;
import com.bdmc.common.annotation.RepeatSubmit;
import com.bdmc.common.core.controller.BaseController;
import com.bdmc.common.core.domain.AjaxResult;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.validate.AddGroup;
import com.bdmc.common.core.validate.EditGroup;
import com.bdmc.common.core.validate.QueryGroup;
import com.bdmc.common.enums.BusinessType;
import com.bdmc.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * 设备管理控制器
 *
 * @author bdmc
 * @date 2022-03-03
 */
@Validated
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/device")
public class BaseDeviceController extends BaseController {

    private final IBaseDeviceService iBaseDeviceService;

    /**
     * 查询设备管理列表
     */
    @SaCheckPermission("base:device:list")
    @GetMapping("/list")
    public TableDataInfo<BaseDeviceVo> list(@Validated(QueryGroup.class) BaseDeviceBo bo, PageQuery pageQuery) {
        return iBaseDeviceService.mySelectPage(bo, pageQuery);
    }

    /**
     * 导出设备管理列表
     */
    @SaCheckPermission("base:device:export")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated BaseDeviceBo bo, HttpServletResponse response) {
        List<BaseDeviceVo> list = iBaseDeviceService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备管理", BaseDeviceVo.class, response);
    }



    @SaCheckPermission("base:device:import")
    @Log(title = "设备管理", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public void importData(@Validated BaseDeviceBo bo, HttpServletResponse response) {
        List<BaseDeviceVo> list = iBaseDeviceService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备管理", BaseDeviceVo.class, response);
    }

    /**
     * 获取设备管理详细信息
     */
    @SaCheckPermission("base:device:query")
    @GetMapping("/{deviceId}")
    public AjaxResult<BaseDeviceVo> getInfo(@PathVariable("deviceId") Long deviceId) {
        return AjaxResult.success(iBaseDeviceService.queryById(deviceId));
    }

    /**
     * 新增设备管理
     */
    @SaCheckPermission("base:device:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody BaseDeviceBo bo) {
        return toAjax(iBaseDeviceService.insertByBo(bo));
    }

    /**
     * 修改设备管理
     */
    @SaCheckPermission("base:device:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody BaseDeviceBo bo) {
        return toAjax(iBaseDeviceService.updateByBo(bo));
    }

    /**
     * 删除设备管理
     */
    @SaCheckPermission("base:device:remove")
    @Log(title = "设备管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceIds}")
    public AjaxResult<Void> remove(@PathVariable Long[] deviceIds) {
        return toAjax(iBaseDeviceService.deleteWithValidByIds(Arrays.asList(deviceIds), true));
    }



}
